#global
#在局部作用域声明一个全局变量
# name='alex'
#
# def func():
#     name='太白金星'
#     print(name)
# func()
# print(name)
# name='alex'

def func():
    global name
    name='太白金星'
    print(name)
print(globals())#获取全局变量
#print(name)#NameError: name 'name' is not defined
func()
print(globals())
#2.修改一个全局变量
count=1
def func():
    # print(count)
    global count
    count+=1
    print(count)
print(count)
func()
print(count)
print('*'*20)

#nonlocal
#1.不能操作全局变量
'''count=1
def func():
    nonlocal count
    count+=1
func()      SyntaxError: no binding for nonlocal 'count' found
'''
#2.局部作用域：内存函数对外层函数的局部变量进行修改
def wrapper():
    count =1
    name='太白'
    lst = []
    def inner():
        nonlocal count
        nonlocal name
        count+=1
        name='alex'
        lst.append(1)
    print(name)
    print(count)
    print(lst)
    inner()
    print(count)
    print(name)
    print(lst)
wrapper()























